package com.hczt.xhminiapp.common.utils;

import java.util.UUID;

/**
 * @author marvin.ma
 * @create 2017-09-15 13:54
 * @desc 随机码工具类
 **/
public class RandomUtil {

    public static final String NUMBER = "1234567890";
    public static final String BASE = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";//使用到Algerian字体，系统里没有的话需要安装字体，字体只显示大写，去掉了1,0,i,o几个容易混淆的字符

    public static String getRandomNumber(boolean numberFlag, int length) {

        StringBuffer retStr = new StringBuffer("");
        String strTable = numberFlag ? NUMBER : BASE;
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr.setLength(0);
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr.append(strTable.charAt(intR));
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr.toString();
    }

    /**
     * 生成UUID
     * @return
     */
    public static String getRandomUUID(){
        return UUID.randomUUID().toString().replace("-", "");
    }
}
